package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.RoleApi;
import com.sd365.permission.centre.entity.Node;
import com.sd365.permission.centre.entity.Role;
import com.sd365.permission.centre.pojo.dto.*;
import com.sd365.permission.centre.pojo.query.RoleQuery;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.*;
import com.sd365.permission.centre.service.RoleService;

import com.sd365.permission.centre.service.exception.UserCentreExceptionCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.ArrayList;

import java.util.List;

/**
 * @program: sd365-permission-centre
 * @description: 角色管理Controller实现类  ，实现RoleApi接口实现界面接口所定义的方法，要求满足
 * 功能需求
 * @author: Mr.Li
 * @create: 2020-12-11 17:03
 **/
@CrossOrigin
@RestController




public class RoleController implements RoleApi  {

    @Autowired
    private RoleService roleService;

    @Override
    public Boolean initRoleResourceRedis(Role role) {
        return roleService.initRoleResourceRedis(role);
    }

    @Override
    public Boolean assignResource(RoleResourceDTO roleResourceDTO) {
        return roleService.assignResource(roleResourceDTO);
    }

    @Override
    public Boolean assignUser(UserRoleDTO userRoleDTO) {
        return roleService.assignUser(userRoleDTO);
    }

    @Override
    public List<Node> doQueryResourceByRoleId(RoleQuery roleQuery) {
        Long roleId = roleQuery.getId();
        return roleService.queryResourceByRoleId(roleId);
    }

    @Override
    public List<Node> selectCompany() {
        return roleService.selectCompany();
    }

    @Override
    public boolean haveRole(RoleDTO roleDTO) {
        return roleService.haveRole(roleDTO);
    }

    @Override
    public Boolean add(RoleDTO roleDTO) {
        return roleService.add(roleDTO);
    }

    @Override
    public Boolean remove(Long id, Long version) {
        return roleService.remove(id,version);
    }

    @Override
    public Boolean batchRemove(@Valid RoleDTO[] roleDTOS) {
        return roleService.batchRemove(roleDTOS);
    }

    @Override
    public Boolean modify(RoleDTO roleDTO) {
        return roleService.modify(roleDTO);
    }

    @Override
    public RoleDTO copy(Long id) {
        RoleDTO roleDTO = roleService.copy(id);
        return roleDTO;
    }

    @Override
    public List<RoleVO> commonQuery(RoleQuery roleQuery) {
        if (roleQuery == null){
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<RoleDTO> roleDTOList = roleService.commonQuery(roleQuery);
        List<RoleVO> roleVOList = BeanUtil.copyList(roleDTOList, RoleVO.class);
        return roleVOList;

    }

    @Override
    public List<UserVO> commonQueryUser(UserQuery userQuery) {
        if (userQuery == null){
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<UserDTO> userDTOList = roleService.commonQueryUser(userQuery);
        List<UserVO> userVOList = BeanUtil.copyList(userDTOList,UserVO.class);
        return  userVOList;
    }

    @Override
    public RoleVO queryRoleById(Long id) {
        RoleDTO roleDTO = roleService.queryById(id);
        if (roleDTO!=null){
            RoleVO roleVO = BeanUtil.copy(roleDTO,RoleVO.class);
            return roleVO;
        }else {
            return null;
        }
    }

    @Override
    public RoleVO queryUserResource(Long id) {
        RoleDTO roleDTO = roleService.queryUserResource(id);
        if (roleDTO!=null){
            RoleVO roleVO = BeanUtil.copy(roleDTO,RoleVO.class);
            return roleVO;
        }else {
            return null;
        }
    }

    @Override
    public RoleCompanyVO queryRoleCompanyById(Long id) {
        RoleCompanyVO roleCompanyVO = roleService.queryRoleCompanyById(id);
        return roleCompanyVO;
    }

    @Override
    public Boolean modifyRoleCompany(RoleCompanyDTO roleCompanyDTO) {
        return roleService.modifyRoleCompany(roleCompanyDTO);
    }

}
